package com.finger.forest;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewpager.widget.ViewPager;

import android.os.Bundle;
import android.widget.ImageView;
import android.widget.TextView;

import com.finger.forest.adapter.SynchronizationDataAdapter;
import com.finger.forest.base.BaseActivity;
import com.finger.forest.base.BaseView;
import com.finger.forest.base.ResponseTag;
import com.finger.forest.fragment.FireFragment;
import com.finger.forest.fragment.ReourceFragment;
import com.finger.forest.fragment.RoadFragment;
import com.finger.forest.http.presenter.RegisterPresenter;
import com.finger.forest.model.CollectionRoadModel;
import com.finger.forest.utils.ToastUtils;
import com.finger.forest.utils.UniversalItemDecoration;
import com.google.android.material.tabs.TabLayout;
import com.jakewharton.rxbinding2.view.RxView;

import org.litepal.LitePal;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import butterknife.BindView;

/**
 * 同步数据
 */
public class SynchronizationDataActivity extends BaseActivity{

    @BindView(R.id.ivLeft)
    ImageView ivLeft;

    @BindView(R.id.tvTitle)
    TextView tvTitle;

    /**
     * 同步到手机
     */
    @BindView(R.id.tvPhone)
    TextView tvPhone;
    /**
     * 同步服务器
     */
    @BindView(R.id.tvService)
    TextView tvService;

    @BindView(R.id.tbTitle)
    TabLayout tbTitle;
    @BindView(R.id.vpResource)
    ViewPager vpResource;

    private String[] titles = new String[]{"道路", "火场", "资源"};

    private List<Fragment> fragmentList = new ArrayList<>();

    private RoadFragment roadFragment;
    private FireFragment fireFragment;
    private ReourceFragment reourceFragment;

    @Override
    protected int setView() {
        return R.layout.activity_synchronization_data;
    }

    @Override
    protected void receiveData() {
        for (int i = 0; i < titles.length; i++) {
            if (i == 0) {
                tbTitle.addTab(tbTitle.newTab().setText(titles[i]));
                roadFragment = new RoadFragment();
                fragmentList.add(i, roadFragment);
            }

            if (i == 1) {
                tbTitle.addTab(tbTitle.newTab().setText(titles[i]));
                fireFragment = new FireFragment();
                fragmentList.add(i, fireFragment);
            }

            if (i == 2) {
                tbTitle.addTab(tbTitle.newTab().setText(titles[i]));
                reourceFragment = new ReourceFragment();
                fragmentList.add(i, reourceFragment);
            }
        }
    }

    @Override
    protected void initializeView() {
        tvTitle.setText("数据同步");


        vpResource.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {
            @NonNull
            @Override
            public Fragment getItem(int position) {
                return fragmentList.get(position);
            }

            @Override
            public int getCount() {
                return fragmentList.size();
            }

            @Nullable
            @Override
            public CharSequence getPageTitle(int position) {
                return titles[position];
            }
        });

        //设置TabLayout和ViewPager联动
        tbTitle.setupWithViewPager(vpResource, false);

        clicks();
    }

    private void clicks() {
        //返回
        RxView.clicks(ivLeft).throttleFirst(2, TimeUnit.SECONDS).subscribe(o -> {
            finish();
        });

        //同步服务器按钮
        RxView.clicks(tvService).throttleFirst(2, TimeUnit.SECONDS).subscribe(o -> {
            tvService.setTextColor(getResources().getColor(R.color.colorBlueTop));
            tvPhone.setTextColor(getResources().getColor(R.color.black3));
        });

        //同步手机
        RxView.clicks(tvPhone).throttleFirst(2, TimeUnit.SECONDS).subscribe(o -> {
            tvPhone.setTextColor(getResources().getColor(R.color.colorBlueTop));
            tvService.setTextColor(getResources().getColor(R.color.black3));
        });
    }

    @Override
    protected void loadDataFromServer() {

    }
}